package org.example.diet_compass.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.diet_compass.domain.Comments;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.Map;

/**
* @author 86135
* @description 针对表【comments(评论表)】的数据库操作Mapper
* @createDate 2025-03-02 17:14:01
* @Entity .domain.Comments
*/
public interface CommentsMapper extends BaseMapper<Comments> {
    @Update("update comments set like_count = like_count + 1 where comment_id = #{commentId}")
    void likeComment(long commentId);
    @Select("select * from comments where user_id = #{userId}")
    List<Comments> getCommentsByUserId(int userId);
    @Select("SELECT c.comment_id,\n" +
            "       c.user_content,\n" +
            "       u.avatar                        AS user_avatar,\n" +
            "       u.nickname                      AS user_name,\n" +
            "       c.user_id,\n" +
            "       c.parent_id,\n" +
            "       c.reply_id,\n" +
            "       ru.nickname                    AS reply_name,\n" +
            "       CASE WHEN (cl.user_id and cl.comment_id ) IS NOT NULL THEN TRUE ELSE FALSE END AS is_like,\n" +
            "       c.like_count,\n" +
            "       c.created_at\n" +
            "FROM comments c\n" +
            "         JOIN users u ON c.user_id = u.id\n" +
            "         LEFT JOIN users ru ON c.reply_id = ru.id\n" +
            "         LEFT JOIN comment_likes cl ON c.comment_id = cl.comment_id\n" +
            "where c.goods_id = #{goodsId}"+
            "GROUP BY c.comment_id, u.avatar, u.nickname, c.user_id, c.parent_id, c.reply_id, ru.nickname, cl.user_id, c.created_at,c.like_count\n" +
            "ORDER BY c.created_at DESC;")
    List<Map<String,Object>> getGoodsComments(String goodsId);

    @Select("select count(goods_id) from comments where goods_id=#{goodId}")
    Integer getGoodCommentTotal(String goodId);

    @Delete("delete from comments where comment_id = #{commentId}")
    void removeByCommentId(Integer commentId);

    @Update("update comments set like_count = like_count + #{Num} where comment_id = #{commentId}")
    void updateLikeNums(Long commentId,int Num);
}




